package com.symaster.common.event;

/**
 * @author yinmiao
 * @version 2022-10-28 14:32
 */
public class DefaultEventResult implements EventResult {
    private Integer resultCode;
    private Object resultData;

    public boolean isSuccess() {
        return resultCode != null && 1 == resultCode;
    }

    public boolean isCancel() {
        return resultCode != null && 2 == resultCode;
    }

    public boolean isError() {
        return resultCode != null && 0 == resultCode;
    }

    public <T> T ofJavaClazz(Class<T> clazz) {
        if (resultCode == null || 1 != resultCode) {
            return null;
        }
        if (!clazz.isInstance(resultData)) {
            return null;
        }
        return clazz.cast(resultCode);
    }

    public String ofString() {
        if (resultData == null) {
            return null;
        }
        return resultData.toString();
    }

    public Object getResultData() {
        return resultData;
    }

    public DefaultEventResult setResultData(Object resultData) {
        this.resultData = resultData;
        return this;
    }

    public Integer getResultCode() {
        return resultCode;
    }

    public DefaultEventResult setResultCode(Integer resultCode) {
        this.resultCode = resultCode;
        return this;
    }

    public static DefaultEventResult cancel() {
        DefaultEventResult defaultEventResult = new DefaultEventResult();
        defaultEventResult.setResultCode(2);
        return defaultEventResult;
    }

    public static DefaultEventResult of(Integer resultCode) {
        DefaultEventResult defaultEventResult = new DefaultEventResult();
        defaultEventResult.setResultCode(resultCode);
        return defaultEventResult;
    }

    public static DefaultEventResult success() {
        DefaultEventResult defaultEventResult = new DefaultEventResult();
        defaultEventResult.setResultCode(1);
        return defaultEventResult;
    }

    public static DefaultEventResult error() {
        DefaultEventResult defaultEventResult = new DefaultEventResult();
        defaultEventResult.setResultCode(0);
        return defaultEventResult;
    }
}
